/**
 *	Request请求到的数据客户端发过来的处理
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framerock.server;

import com.framerock.baselib.Rock;
import java.net.URLDecoder;
import java.util.Map;

/**
 * Request请求到的数据客户端发过来的处理
 * */
public class Request {

    private HttpClient clientSocket;
    private Map<String,String> getData, postData, Logininfo, Companyinfo,Userainfo;

    /**
     * 当前用户ID
     * */
    public String USERID        = "0";

    /**
     * 对应单位下的单位用户ID
     * */
    public String USEAID        = "0";

    /**
     * 对应单位下用户的姓名
     * */
    public String USEANAME      = "";

    /**
     * 是否设置单位用户
     * */
    public Boolean USERABOOL    = false;


    /**
     * 单位用户级别 0普通用户,1管理员
     * */
    public int USEATYPE         = 0;

    /**
     * 加载次数
     * */
    public int LOADCI           = 0;

    /**
     * 单位ID
     * */
    public String COMPANYID     = "0";

    /**
     * 是否单位模式
     * */
    public Boolean COMPANYBOOL  = false;

    /**
     * 平台用户类型0普通用户,1管理员,2超级管理员
     * */
    public int USERTYPE         = 0;


    public Request(HttpClient clientSockets) {
        clientSocket = clientSockets;
        getData      = Rock.getMap();
        postData     = Rock.getMap();
        String path  = getHeader("Path"),val = "",arr[],arr1[],arr2[];
        if(!Rock.isEmpt(path)){
            arr = path.split("\\?");
            if(arr.length>1){
                arr1 = arr[1].split("&");
                for(int i=0;i<arr1.length;i++){
                    arr2 = arr1[i].split("=");
                    val  = "";
                    if(arr2.length>1)val = arr2[1];
                    getData.put(arr2[0], val);
                }
            }
        }

        path = getHeader("postdata");
        if(!Rock.isEmpt(path)){
            arr1 = path.split("&");
            for(int i=0;i<arr1.length;i++){
                arr2 = arr1[i].split("=");
                val  = "";
                if(arr2.length>1)val = arr2[1];
                postData.put(arr2[0], val);
            }
        }
        LOADCI  = Integer.parseInt(get("loadci", "1"));
    }

    /**
     * 获取请求header
     * */
    public String getHeader(String key)
    {
        if(clientSocket==null)return "";
        return clientSocket.getHeader(key);
    }

    /**
     * 获取请求浏览器信息
     * */
    public String getUserAgent()
    {
        if(clientSocket==null)return "";
        return clientSocket.getUserAgent();
    }

    /**
     * 获取所有get的数据
     * */
    public Map<String,String> get()
    {
        return this.getData;
    }

    /**
     * 获取所有post的数据
     * */
    public Map<String,String> post()
    {
        return this.postData;
    }

    /**
     * 获取url参数
     * @param key 参数
     * */
    public String get(String key)
    {
        return this.get(key, "");
    }

    /**
     * 获取url参数
     * @param key 参数
     * @param dev 默认值
     * */
    public String get(String key, String dev)
    {
        String str = Rock.getMapString(this.getData, key, dev);
        return URLDecoder.decode(str);
    }

    /**
     * 获取URL参数数字
     * @param key 参数
     * */
    public int getInt(String key)
    {
        return Integer.parseInt(get(key, "0"));
    }

    /**
     * 获取post数据
     * @param key 参数
     * */
    public String post(String key)
    {
        return this.post(key, "");
    }

    /**
     * 获取post数据
     * @param key 参数
     * @param dev 默认值
     * */
    public String post(String key, String dev)
    {
        String str = Rock.getMapString(this.postData, key, dev);
        return URLDecoder.decode(str);
    }

    /**
     * 获取post数据数字
     * @param key 参数
     * */
    public int postInt(String key)
    {
        return Integer.parseInt(post(key, "0"));
    }

    /**
     * 获取所有post数据
     * */
    public String postdata()
    {
        return getHeader("postdata");
    }

    /**
     * 获取IP
     * */
    public String getIP()
    {
        String str = this.getHeader("X-FORWARDED-FOR");
        if(Rock.isEmpt(str) || str.equals("unknow"))str = this.getHeader("ip");
        return str;
    }

    /**
     * 获取浏览器类型
     * */
    public String getWeb()
    {
        String str = this.getUserAgent();
        String iwarr[] = {"MSIE", "Safari", "Chrome", "iPhone", "Android", "Edge", "MQQBrowser", "MicroMessenger","wxwork"};
        String wz  = "unkown";
        for(int i=0;i<iwarr.length;i++){
            if(Rock.contain(str, iwarr[i])) {
                wz = iwarr[i];
            }
        }
        return wz;
    }

    /**
     * 获取Host的
     * */
    public String getHost()
    {
        return getHeader("Host");
    }

    /**
     * 设置登录信息
     * */
    public void setLogininfo(Map<String, String> da)
    {
        this.Logininfo  = da;
        this.USERID     = Rock.getMapString(da, "id", "0");
        this.USERTYPE   = Rock.getMapint(da, "type");
    }

    /**
     * 获取登录值
     * @param key 关键词如name
     * */
    public String getLoginrs(String key)
    {
        return Rock.getMapString(this.Logininfo, key);
    }

    /**
     * 获取平台用户登录信息
     * */
    public Map<String, String> getLogininfo()
    {
        return this.Logininfo;
    }

    /**
     * 设置单位信息
     * */
    public void setCompanyinfo(Map<String, String> da)
    {
        this.Companyinfo    = da;
        this.COMPANYID      = Rock.getMapString(da, "id", "0");
        this.COMPANYBOOL    = true;
    }
    public String getCompanyrs(String key)
    {
        return Rock.getMapString(this.Companyinfo, key);
    }
    public Map<String, String> getCompanyinfo()
    {
        return this.Companyinfo;
    }

    /**
     * 设置单位用户信息
     * */
    public void setUserainfo(Map<String, String> da)
    {
        this.Userainfo    = da;
        if(da != null && Rock.getMapint(da, "status")==1 && Rock.getMapint(da, "state")==1) {
            this.USERABOOL = true;
            this.USEAID = Rock.getMapString(da, "id", "0");
            this.USEANAME = Rock.getMapString(da, "name");
            this.USEATYPE = Rock.getMapint(da, "type");
        }
    }

    /**
     * 获取登录单位用户信息key
     * @param key 字段，如name,id
     * */
    public String getUserars(String key)
    {
        return Rock.getMapString(this.Userainfo, key);
    }

    /**
     * 获取登录单位用户全部信息
     * */
    public Map<String, String> getUserainfo()
    {
        return this.Userainfo;
    }


    /**
     * 步骤错误信息的
     * */
    private String errorString = "";
    public void setError(String msg) {setError(msg, true);}
    public void setError(String msg, Boolean join) {
        if(join) {
            if (this.isError()) errorString += ";";
            errorString += msg;
        }else{
            errorString = msg;
        }
    }
    public String getError(){ return errorString; }
    public Boolean isError(){if(Rock.isEmpt(errorString))return false;return true;}
}
